/* needmask */

var HooPopup = new Class ({
	options: {
		width: 500,
		height: 90,
		autoresize: true,
		clsWindow: 'hoopopup',
		clsTitle: 'hoopopup-title',
		clsContent: 'hoopopup-content',
		clsContentLoading: 'waiting',
		duration: 500
	},
	
	window: undefined,
	visible: false,
	
	initialize: function (options) {
		if ($defined(options)) this.options = $merge(this.options, options);
	},
	
	clear: function (options) {
		if (this.window !== undefined) {
			if ($defined(this.window._title)) this.window._title.empty();
			if ($defined(this.window._content)) this.window._content.empty();
		}
	},
	
	show: function (content, options) {
		//content type: element|ajax|html|url options.type
		if (!$defined(options)) options = {};
		var type = $defined(options.type)?options.type:false;
		if (!type) {
			if ($type(content) == 'element') type = 'element';
			else type='html';
		}
		if (!type) return;
		var body = $$('body')[0];
		body.HooMaskSet('hoobodymask', 0);
		body._loading.addEvent('click', function() {this.hide();}.bind(this));
		//body.addEvent('click', function() {this.hide();}.bind(this));
		if (this.window === undefined) {
			this.window = new Element('div').injectInside($$('body')[0]);
			this.window.setStyles({
					display: 'block',
					'z-index': 999,
					position: 'fixed',
					top: -this.options.height,
					width: this.options.width,
					height: this.options.height,
					overflow: 'hidden'
			});
			this.window.addClass(this.options.clsWindow);
		}
		if (!$defined(this.window._title)) {
			this.window._title = new Element('div').injectInside(this.window);
			this.window._title.addClass(this.options.clsTitle);
			this.window._title._window = this;
		} else this.window._title.empty();
		if (!$defined(this.window._content)) {
			this.window._content = new Element('div').injectAfter(this.window._title);
			this.window._content.addClass(this.options.clsContent);
			this.window._content._window = this;
		} else this.window._content.empty();
		this.window._content.HooMaskSet(this.options.clsContentLoading);
		this.visible = true;
		this.slide(true); 
		this.resize(this.options.width, this.options.height);
		if ($defined(options.beforeLoad)) options.beforeLoad(this);
		
		if ($defined(options.title)) this.window._title.set('html', options.title);
		var proceedcomplete = true;
		switch (type) {
		case 'element':
			this.window._content.set('html', content.get('html'));
			break;
		case 'html':
			this.window._content.set('html', content);
			break;
		case 'url':
			iframe = new Element('iframe', {width: '100%', height: '100%'}).injectInside(this.window._content);
			iframe.src = content;
			break;
		case 'ajax':
			proceedcomplete = false;
			var opt = {
				url: content,
				method: $defined(options.method)?options.method:'post',
				evalScripts: true,
				update: this.window._content,
				onSuccess: function () {
					if ($defined(options.postSuccess)) options.postSuccess();
					this.resize($defined(options.width)?options.width:this.options.width, options.height, false, function() {
						this.window._content.HooMaskRemove();
						if ($defined(options.afterLoad)) options.afterLoad(this);			
					}.bind(this));
				}.bind(this)
			}
			if ($defined(options.data)) opt.data = options.data;
			//if ($defined(options.postSuccess)) opt.postSuccess = options.postSuccess;
			if ($defined(options.beforeRequest)) opt.beforeRequest = options.beforeRequest;
			new Request.HTML(opt).send();
			break;
		}
		if (proceedcomplete) {
			this.resize($defined(options.width)?options.width:this.options.width, options.height, false, function() {
				this.window._content.HooMaskRemove();
				if ($defined(options.afterLoad)) options.afterLoad(this);			
			}.bind(this));
		}
	},
	
	hide: function (quick) {
		if (this.window == undefined || this.visible == false) return;
		this.slide(false);
		var body = $$('body')[0];
		if ($defined(body._loading)) body._loading.removeEvent('click');
		body.HooMaskRemove();		
		/*this.resize(undefined, 0, $defined(quick)?quick:false, function() {
			this.window.visible = false;

			//this.clear();
			//this.window.setStyle('display', 'none');
		}.bind(this));*/
	},
	
	deactivate: function () {
		if (this.window == undefined || this.visible == false) return;
		this.window.HooMaskSet('disabled');
	},
	
	activate: function () {
		if (this.window == undefined || this.visible == false) return;
		this.window.HooMaskRemove();
	},
	
	resize: function (width, height, noeffects, complete) {
		if (this.window == undefined || this.visible == false) return;
		if ($defined(this.window._effect)) {this.window._effect.cancel()}
		var windowSize = this.window.getSize();
		var bodySize = $$('body')[0].getSize();
		this.window.setStyles({
			left: (bodySize.x - windowSize.x) / 2,
			display: 'block',
			position: 'absolute'
		});
		var titleSize = this.window._title.getSize();
		var contentSize = this.window._content.getSize();
		var newwidth = $defined(width)?width:(titleSize.x>contentSize.x?(windowSize.x>titleSize.x?windowSize.x:contentSize.x):contentSize.x);
		var newheight = $defined(height)?height:(titleSize.y+contentSize.y);
		var newleft = (bodySize.x - newwidth)/2;
		if ($defined(noeffects) && noeffects) {
			this.window.setStyles({
				height: newheight,
				width: newwidth,
				left: newleft
			});
		} else {
			if (!$defined(this.window._effect)) {
				this.window._effect = new Fx.Morph(this.window, {duration: this.options.duration, transition: Fx.Transitions.Sine.Elastic});
			}
			this.window._effect.start({width: newwidth, height: newheight, left: newleft, 
				onComplete: function() {
					if ($defined(complete)) complete();
				}
			});
		}
	},
	
	slide: function(forward, noeffects) {
		if (this.window == undefined || this.visible == false) return;		
		if ($defined(this.window._slide)) {this.window._slide.cancel();}
		if (forward) {
			newtop = 0;
		} else {
			if ($defined(this.window._effect)) {this.window._effect.cancel();}
			newtop = -this.window.getSize().y;
		}
		this.window.setStyle('left', ($$('body')[0].getSize().x - this.window.getSize().x)/2);
		if ($defined(noeffects) && noeffects) {
			this.window.setStyles({
				top: newtop
			});
		} else {
			if (!$defined(this.window._slide)) {
				this.window._slide = new Fx.Morph(this.window, {duration: this.options.duration, transition: Fx.Transitions.Sine.Elastic});
			}
			this.window._slide.start({top: newtop});
		}
	}
});

HooPopup.implement(new Events);